package com.csun.cmny.provider.utils;

import com.csun.cmny.provider.model.constant.DeviceConstant;
import com.csun.cmny.provider.model.constant.DmcConstant;
import com.csun.cmny.provider.model.dto.device.DeviceBindDto;
import com.csun.cmny.provider.model.vo.OldmanBedRoomBuildingVo;
import com.csun.cmny.provider.model.vo.RoomBuildingVo;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.RedisKeyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-03-21 16:49
 * @Description: the class device utils
 */
@Component
public class DeviceUtils {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    public OldmanBedRoomBuildingVo getOldmanBedRoomBuildingVo(DeviceBindDto deviceBindDto) {

        OldmanBedRoomBuildingVo oldmanBedRoomBuildingVo = null;
        RoomBuildingVo rbv;
        if (deviceBindDto.getBindType() == null) {
            return oldmanBedRoomBuildingVo;
        }

        switch (deviceBindDto.getBindType()) {
            case DmcConstant.CSUN_SLEEP_BIND_TYPE_OLDMAN :
                if (PublicUtil.isEmpty(deviceBindDto.getOldmanId())) {
                    return null;
                }
                OldmanBedRoomBuildingVo obv = (OldmanBedRoomBuildingVo) redisTemplate
                        .opsForValue().get(RedisKeyUtil.getOmcOldmanId(deviceBindDto.getOldmanId()));
                oldmanBedRoomBuildingVo = obv;
                break;
            case DmcConstant.CSUN_BUTTON_BIND_TYPE_BED :
                if (PublicUtil.isEmpty(deviceBindDto.getBedId())) {
                    return null;
                }
                OldmanBedRoomBuildingVo bov = (OldmanBedRoomBuildingVo) redisTemplate
                        .opsForValue().get(RedisKeyUtil.getOmcBedId(deviceBindDto.getBedId()));
                oldmanBedRoomBuildingVo = bov;
                break;
            case DmcConstant.CSUN_BUTTON_BIND_TYPE_ROOM :
                if (PublicUtil.isEmpty(deviceBindDto.getBedId())) {
                    return null;
                }
                OldmanBedRoomBuildingVo rbo = (OldmanBedRoomBuildingVo) redisTemplate
                        .opsForValue().get(RedisKeyUtil.getOmcRoomId(deviceBindDto.getRoomId()));
                oldmanBedRoomBuildingVo = rbo;
                break;
            case DeviceConstant.DEVICE_BIND_TYPE_LIFE :
                if (PublicUtil.isEmpty(deviceBindDto.getRoomId())) {
                    return null;
                }
                rbv = (RoomBuildingVo) redisTemplate
                        .opsForValue().get(RedisKeyUtil.getOmcRoomId(deviceBindDto.getRoomId()));
                oldmanBedRoomBuildingVo = new OldmanBedRoomBuildingVo();
                BeanUtils.copyProperties(rbv, oldmanBedRoomBuildingVo);
                break;
        }

        return oldmanBedRoomBuildingVo;
    }

    public RoomBuildingVo getRoomBuildingVo(DeviceBindDto deviceBindDto) {

        RoomBuildingVo roomBuildingVo = null;

        if (PublicUtil.isNotEmpty(deviceBindDto.getBindType())&&deviceBindDto.getBindType()!=null) {
            if (DmcConstant.CSUN_BODU_SENSOR_BIND_TYPE_ROOM == (int) deviceBindDto.getBindType()) {
                RoomBuildingVo rbv = (RoomBuildingVo) redisTemplate
                        .opsForValue().get(RedisKeyUtil.getOmcRoomId(deviceBindDto.getRoomId()));
                if (PublicUtil.isNotEmpty(rbv)) {
                    roomBuildingVo = rbv;
                }
            }
            if (DmcConstant.CSUN_BUTTON_BIND_TYPE_ROOM == (int) deviceBindDto.getBindType()) {
                RoomBuildingVo rbv = (RoomBuildingVo) redisTemplate
                        .opsForValue().get(RedisKeyUtil.getOmcRoomId(deviceBindDto.getRoomId()));
                if (PublicUtil.isNotEmpty(rbv)) {
                    roomBuildingVo = rbv;
                }
            }
            /**
             * @date    2020-01-06
             * @author  chisj
             * @desc    新增life房间绑定
             */
            if (DeviceConstant.DEVICE_BIND_TYPE_LIFE == (int) deviceBindDto.getBindType()) {
                RoomBuildingVo rbv = (RoomBuildingVo) redisTemplate
                        .opsForValue().get(RedisKeyUtil.getOmcRoomId(deviceBindDto.getRoomId()));
                if (PublicUtil.isNotEmpty(rbv)) {
                    roomBuildingVo = rbv;
                }
            }
        }

        return roomBuildingVo;
    }
}
